package cn.itcast.lambda;

import cn.itcast.entity.UserEntity;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.function.Consumer;

public class Test01 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三",18));
        userEntities.add(new UserEntity("李四",26));
        userEntities.add(new UserEntity("王二",10));
        userEntities.add(new UserEntity("麻子",28));

        //遍历ArrayList集合
        userEntities.forEach(new Consumer<UserEntity>() {
            @Override
            public void accept(UserEntity userEntity) {
                System.out.println(userEntity);
            }
        });
        //改为lambda表达式
        System.out.println("---------------------------------------------------------------");
        System.out.println("改为lambda表达式写  ");
        userEntities.forEach(userEntity -> System.out.println(userEntity));

        System.out.println("--------------------------------------------------------");
        System.out.println("对集合里面的数据根据年龄排序");
        userEntities.sort(new Comparator<UserEntity>() {
            @Override
            public int compare(UserEntity o1, UserEntity o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println("遍历排序后的集合");
        System.out.println("--------------------------------------------------------");
        userEntities.forEach( userEntity -> System.out.println(userEntity));
        System.out.println("--------------------------------------------------------");




    }
}
